$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/useupgradelog/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 30, key: true },
            // { label: '操作', name: 'status', width: 50, formatter: function(value, options, row){
            //         var rowId=options.rowId;
            //         if(value == 0){
            //             return  '<span style="color: #0d6aad" onclick=auditRecord('+rowId+','+value+');>审核</span>';
            //         }else if(value == 1){
            //             return  '<span style="color: #0d6aad" onclick=auditRecord('+rowId+','+value+');>查看</span>';
            //         }else {
            //             return  '<span style="color: #0d6aad" onclick=auditRecord('+rowId+','+value+'); >查看原因</span>';
            //         }
            //     }},
			{ label: '用户ID', name: 'userId', index: 'user_id', width: 80 }, 			
			// { label: '姓名', name: 'userName', index: 'userName', width: 80 },
			{ label: '手机号', name: 'mobile', index: 'mobile', width: 80 },
			// { label: '类型', name: 'type', index: 'type', width: 80 ,
            //     formatter: function(value, options, row){
            //         return typeSwitch(value);
            //     }
            // },
			{ label: '当前等级', name: 'level', index: 'level', width: 80,hidden:true },
			{ label: '当前等级名称', name: 'levelName', index: 'level_name', width: 80 }, 			
			{ label: '升级后的等级', name: 'upgradeLevel', index: 'upgrade_level', width: 80 }, 			
			{ label: '升级后的等级名称', name: 'upgradeLevelName', index: 'upgrade_level_name', width: 80 }, 			
			{ label: '推荐数量', name: 'recommendNum', index: 'recommend_num', width: 80 },
			{ label: '累计业绩', name: 'performance', index: 'performance', width: 80 },
			// { label: '推荐同级累计进货金额', name: 'recommendPerformance', index: 'recommend_performance', width: 80 },
			{ label: '备注', name: 'remark', index: 'remark', width: 80 ,hidden:true},
			{ label: '创建时间', name: 'createTime', index: 'create_time', width: 120 },
			{ label: '修改时间', name: 'updateTime', index: 'update_time', width: 120 }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    $( "#createTime" ).datepicker({
        showOtherMonths: true,
        selectOtherMonths: true,
        maxDate:new Date()
    });
    $( "#createTime1" ).datepicker({
        showOtherMonths: true,
        selectOtherMonths: true,
        maxDate:new Date()
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
        q:{
            userId: null,
            createTime: null,
            createTime1: null
        },
		showList: true,
		title: null,
		useupgradeLog: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.useupgradeLog = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = vm.useupgradeLog.id == null ? "sys/useupgradelog/save" : "sys/useupgradelog/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.useupgradeLog),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "sys/useupgradelog/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
		getInfo: function(id){
			$.get(baseURL + "sys/useupgradelog/info/"+id, function(r){
                vm.useupgradeLog = r.useupgradeLog;
            });
		},
        reset:function(){
            vm.q = {userId:null, createTime:null, createTime1:null};
            $("input[name='qInput']").val('');
            vm.reload();

        },
		reload: function (event) {
		    debugger;
			vm.showList = true;
            var createTime =   $("#createTime").val();
            var createTime1 = $("#createTime1").val();
            if (createTime.length > 0 && createTime1.length > 0) {
                var startTmp = createTime.split("-");
                var endTmp = createTime1.split("-");
                var sd = new Date(startTmp[0], startTmp[1], startTmp[2]);
                var ed = new Date(endTmp[0], endTmp[1], endTmp[2]);
                if (sd.getTime() > ed.getTime()) {
                    alert("开始日期不能大于结束日期");
                    return ;
                }
            }
            if(createTime !=null && createTime != ''){
                createTime += " 00:00:00";
            }
            if(createTime1 !=null && createTime1 != ''){
                createTime1 += " 23:59:59";
            }
            parmas=	{'userId':vm.q.userId,'mobile':vm.q.mobile,'createTime':createTime,'createTime1':createTime1};
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:parmas,
                page:1
            }).trigger("reloadGrid");
		}
	}
});

function auditRecord(rowId,status) {
    if(status==0){
        $("#btn_no").show();
        $("#btn_ok").show();
        $("#btn_remove").hide();
    }else{
        $("#btn_no").hide();
        $("#btn_ok").hide();
        $("#btn_remove").show();
    }
    $("#failCauseDiv").hide();
    var rowData=jQuery("#jqGrid").jqGrid("getRowData",rowId);
    $("#recordId").val(rowData.id);
    $("#userId").html(rowData.userId);
    $("#userName").html(rowData.userName);
    $("#mobile").html(rowData.mobile);
    $("#type").html(rowData.type);
    $("#levelName").html(rowData.levelName);
    $("#upgradeLevelName").html(rowData.upgradeLevelName);
    $("#recommendNum").html(rowData.recommendNum);
    $("#performance").html(rowData.performance);
    $("#recommendPerformance").html(rowData.recommendPerformance);
    $("#status1").html(statusSwitch(status));
    if(status==2){
        $("#failCauseDiv").show();
        $("#remark").html(rowData.remark);
    }
    $('#myModal').modal();
}
function updateOut(status){
    debugger;
    var id=$("#recordId").val();
    if(status==2){
        $('#failCauseModal').modal();
    }else if(status==1){
        savePublicOut(status,id,null)
    }
}
function savefailCause(status){
    var failCause=$("#remark1").val();
    var id=$("#recordId").val();
    savePublicOut(status,id,failCause)
}
function savePublicOut(status,recordId,failCause){

    url = baseURL + "sys/useupgradelog/auditRecord";
    var updateModel = {id:recordId,status:status,remark:failCause};
    $.ajax({
        type: "POST",
        url: url,
        contentType: "application/json",
        data: JSON.stringify(updateModel),
        success: function(r){
            if(r.code == 0){
                alert('操作成功', function(){
                    vm.reload();
                });
            }else{
                alert(r.msg);
            }
        }
    });
}
function statusSwitch(value){
    if(value == 0){
        return  '<span >审核中</span>';
    }else if(value == 1){
        return  '<span >审核通过</span>';
    }else if(value == 2){
        return  '<span >审核拒绝</span>';
    }else{
        return  '<span > </span>';
    }
}
function typeSwitch(value){
    if(value == 1){
        return  '<span >推荐晋升</span>';
    }else if(value == 2){
        return  '<span >累计金额晋升</span>';
    }else{
        return  '<span > </span>';
    }
}